<template>
    <div v-loading="loading">
        <CommonPage :state="state" :flowId="infoData.flowId" @submitForm="submitForm">
            <Info style="padding: 20px" ref="form" v-model="infoData" :state="state"></Info>
        </CommonPage>
<!--        <div v-if="'write'==state" class="card-wrap flex justify-end card-padding">-->
<!--            <el-button :loading="loading" type="primary" @click="submitForm(0)">保存</el-button>-->
<!--            <el-button @click="cancel">取消</el-button>-->
<!--            <el-button :loading="loading" type="primary" @click="submitForm(1)">提交</el-button>-->
<!--        </div>-->

    </div>
</template>

<script>


import {
    supplierBidDetail,
    supplierBidUpdate,
    supplierBidAdd,
    supplierDetail,
    supplierUpdate, supplierAdd, supplierBidFlowAdd
} from "@/api/memberManage/vendorList";
import Info from "@/views/vendorManage/tenderVendorList/components/info.vue";


export default {
    name: "tenderVendorAdd",
    components: {Info},
    data() {
        return {
            loading: false,
            infoData: {},
            state: 'write'
        };
    },
    watch:{
        '$route':{
            handler(val){
                console.log('this.$route', this.$route)

                if(val && val.name==this.$options.name){
                    if (this.$route.query && this.$route.query.supplierId && !this.$route.query.tabClick) {
                        this.getOldData(this.$route.query.supplierId)
                    }
                    else if(!this.$route.query.tabClick){
                        this.infoData = {}
                    }
                    this.state = this.$route?.query?.type

                    const title =  this.$route?.query?.pageType??'投标供应商'
                    const route = Object.assign({}, this.$route, { title: `${title}` })
                    this.$store.dispatch('tagsView/updateVisitedView', route)
                }
            },
            immediate:true,
            deep:true
        }
    },
    // mounted() {
    //     console.log('==>', this.$route.query)
    //     if (this.$route.query && this.$route.query.supplierId) {
    //         this.getOldData(this.$route.query.supplierId)
    //     }
    //     this.state = this.$route?.query?.type
    //
    //     const title =  this.$route?.query?.pageType??'投标供应商'
    //     const route = Object.assign({}, this.$route, { title: `${title}` })
    //     this.$store.dispatch('tagsView/updateVisitedView', route)
    // },
    methods: {
        cancel() {
            this.$router.back();
            this.$store.dispatch('tagsView/delView', this.$route);
        },
        getOldData(supplierId) {
            this.loading = true;
            supplierBidDetail(supplierId).then(res => {
                this.infoData = res.data
            }).finally(() => {
                this.loading = false;
            });
        },
        async submitForm(type) {
            let that = this

            console.log('form', this.infoData)

            this.infoData.approvalStatus = type

            if (0 == type) {
                that.commit()
            } else if (1 == type) {
                let result = await this.$refs.form.valid();
                // this.$refs["form"].validate(valid => {
                    if (result) {
                        // that.commit()
                        that.flowCommit()
                    }
                // });
            }

        },
        flowCommit(){

            let that = this
            this.loading = true;

            supplierBidFlowAdd(that.infoData).then(res => {
                this.loading = false;
                this.$modal.msgSuccess("提交成功");
                this.$router.back();
this.$store.dispatch('tagsView/delView', this.$route);
            }).finally(e => {
                this.loading = false;
            })
        },

        commit() {
            let that = this
            this.loading = true;

            if (that.infoData.supplierId) {
                supplierBidUpdate(that.infoData).then(res => {
                    this.loading = false;
                    this.$modal.msgSuccess("保存成功");
                    this.$router.back();
this.$store.dispatch('tagsView/delView', this.$route);
                }).finally(e => {
                    this.loading = false;
                })
            } else {
                supplierBidAdd(that.infoData).then(res => {
                    this.loading = false;
                    this.$modal.msgSuccess("保存成功");
                    this.$router.back();
this.$store.dispatch('tagsView/delView', this.$route);
                }).finally(e => {
                    this.loading = false;
                })
            }
        },

    }
};
</script>


<style lang="scss" scoped>
::v-deep .el-form--label-top .el-form-item__label {
    padding: 0;
}

.el-form-item {
    margin-bottom: 15px;
    width: 70%;
}

::v-deep .el-date-editor--daterange {
    width: 100% !important;
}

::v-deep .el-select {
    width: 100%;
}

::v-deep .el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 100%;
}


</style>
